package com.dream.inspect.modules.business.controller;

import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.modules.business.entity.SysMessageEntity;
import com.dream.inspect.modules.business.service.SysMessageService;
import com.dream.inspect.modules.sys.controller.AbstractController;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;



/**
 * 系统消息表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-16 17:04:51
 */
@RestController
@RequestMapping("business/sysmessage")
public class SysMessageController  extends AbstractController{
    @Autowired
    private SysMessageService sysMessageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("business:sysmessage:list")
    public R list(@RequestParam Map<String, Object> params){
        //PageUtils page = sysMessageService.queryPage(params);
    	params.put("dqyhId", getUserId());
    	params.put("dqbmId", getDeptId());
    	params.put("dqjsId", getSessionUser().get("roleId"));
    	PageUtils page = sysMessageService.findSysMsg(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("business:sysmessage:info")
    public R info(@PathVariable("id") Long id){
        //SysMessageEntity sysMessage = sysMessageService.selectById(id);
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("dqyhId", getUserId());
    	params.put("dqbmId", getDeptId());
    	params.put("dqjsId", getSessionUser().get("roleId"));
    	params.put("id", id);
    	PageUtils page = sysMessageService.findSysMsg(params);
        return R.ok().put("data", page.getList().get(0));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:sysmessage:save")
    public R save(@RequestBody SysMessageEntity sysMessage){
        sysMessageService.insert(sysMessage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:sysmessage:update")
    public R update(@RequestBody SysMessageEntity sysMessage){
        ValidatorUtils.validateEntity(sysMessage);
        sysMessageService.updateAllColumnById(sysMessage);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:sysmessage:delete")
    public R delete(@RequestBody Long[] ids){
        sysMessageService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    
    @RequestMapping("/getNum")
    public R getNum(@RequestParam Map<String, Object> params){
    	params.put("dqyhId", getUserId());
    	params.put("dqbmId", getDeptId());
    	params.put("dqjsId", ";" + getSessionUser().get("roleId") + ";");
    	PageUtils page = sysMessageService.findSysMsg(params);
    	return R.ok().put("data", page.getTotalCount());
    }
    /**
     * 阅读
     * @param params
     * @return
     */
    @RequestMapping("/read")
    public R read(@RequestParam Map<String, Object> params){
    	if(!params.containsKey("targetId")){
    		return R.error("请输入系统消息ID!");
    	}
    	params.put("typeFlag", "3");
    	params.put("userId", getUserId());
    	params.put("deptId", getDeptId());
    	return sysMessageService.read(params);
    }
}
